/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.hed.creature;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author fredrik
 */
public class WoodsMenLearnStrategi implements LearnStrategy {

    public static CollectingSkill LUMBERJACK = new CollectingSkill("LUMBERJACK", "WOOD", 2);
    public static CollectingSkill HUNTER = new CollectingSkill("HUNTER", "FOOD", 3);
    public static CollectingSkill FARMER = new CollectingSkill("FARMER", "FOOD", 2);
    public static CollectingSkill FISHER = new CollectingSkill("FISHER", "FOOD", 3);
    public static CollectingSkill CLAYFETCHER = new CollectingSkill("CLAYFETCHER", "CLAY", 2);
    public static CollectingSkill CARPENTER = new CollectingSkill("CARPENTER", "HOUSE", 1);
    public static CollectingSkill CARRYMAN = new CollectingSkill("CARRYMAN", "STONE", 1);
    public static CollectingSkill COALMINER = new CollectingSkill("COALMINER", "COAL", 1);
    public static CollectingSkill MINER = new CollectingSkill("MINER", "MINE", 1);
    public static CollectingSkill BRICKMAKER = new CollectingSkill("BRICKMAKER", "BRICK", 1);

    private Random r = new Random();

    public Set<Skill> possibleLearnings(Map<String, Skill> knowlegde) {
        Set<Skill> skills = new HashSet<Skill>();

        if (!knowlegde.containsKey("LUMBERJACK")) {
            skills.add(LUMBERJACK);
        }
        if (!knowlegde.containsKey("HUNTER")) {
            skills.add(HUNTER);
        }
        if (!knowlegde.containsKey("FARMER")) {
            skills.add(FARMER);
        }
        if (!knowlegde.containsKey("FISHER")) {
            skills.add(FISHER);
        }
        if (!knowlegde.containsKey("CLAYFETCHER")) {
            skills.add(CLAYFETCHER);
        }

        if (knowlegde.containsKey("LUMBERJACK")) {
            skills.add(CARPENTER);
            skills.add(COALMINER);
        }
        if (knowlegde.containsKey("CARPENTER")) {
            skills.add(CARRYMAN);
            skills.add(MINER);
            skills.add(BRICKMAKER);
        }

        return skills;
    }

    public Skill selectBestLearning(Set<Skill> possibleLearnings, Map<String, Skill> knowlegde) {
        if (possibleLearnings == null) {
            return null;
        }

        Collection<Skill> knownSkills = knowlegde.values();
        possibleLearnings.removeAll(knownSkills);

        if (possibleLearnings.size() == 0) {
            return null;
        }

        Random r = new Random();
        int nr = r.nextInt(possibleLearnings.size());
        Skill skill = null;
        for (Iterator<Skill> it = possibleLearnings.iterator(); it.hasNext();) {
            skill = it.next();
            if (nr == 0) {
                break;
            }
            nr--;
        }
        return skill;
    }

    public Map<String, Integer> getNeed(Group group) {
        Map<String, Integer> need = new HashMap<String, Integer>();

        // FOOD
        int foodAvailable = getValue(group.getWealth(), "FOOD");

        if (group.getNbrMembers() > foodAvailable) {
            need.put("FOOD", 2*(group.getNbrMembers() - foodAvailable));
        }

        // WOOD
        if (group.getNbrMembers() > getValue(group.getWealth(), "WOOD")) {
            addValue(need, "WOOD", group.getNbrMembers() - getValue(group.getWealth(), "WOOD"));
        }


        // HOUSE
        if (group.getNbrMembers()/10 > getValue(group.getWealth(), "HOUSE")) {
            need.put("HOUSE", group.getNbrMembers()/10 - getValue(group.getWealth(), "HOUSE"));

            if (need.get("HOUSE")*10 > getValue(group.getWealth(), "WOOD")) {
                addValue(need, "WOOD", (need.get("HOUSE")*10 - getValue(group.getWealth(), "WOOD")));
            }

            if (need.get("HOUSE")*5 > getValue(group.getWealth(), "STONE")) {
                addValue(need, "STONE", (need.get("HOUSE")*5 - getValue(group.getWealth(), "STONE")));
            }
        }

        if (!need.isEmpty()) {
//            System.out.println("---- need identified " + need);
        }

        return need;
    }

    private int getValue(Map<String, Integer>map, String key) {
        if (map.containsKey(map)) {
            return map.get(map).intValue();
        }
        return 0;
    }

    private int addValue(Map<String, Integer>map, String key, int value) {
        int result = 0;
        if (map.containsKey(map)) {
            result = map.get(map).intValue() + value;
            map.put(key, map.get(map).intValue() + value);
        }
        else {
            result = value;
            map.put(key, value);
        }
        return result;
    }

    public Skill mostNeededSkill(Collection<Skill> knownSkills, Map<String, Integer> need) {
        if (knownSkills == null) {
            throw new RuntimeException("No known skills");
        }

        String maxNeed = null;
        int max = 0;
        for (String resource : need.keySet()) {
            if (need.get(resource).intValue() > max) {
                max = need.get(resource).intValue();
                maxNeed = resource;
            }
        }

        for (Skill skill : knownSkills) {
            if (skill.getName().equals(maxNeed)) {
                System.out.print(" - - - - mostNeededSkill:"  + skill.getName());
                return skill;
            }
        }

        // no special need, just pick one...
        int next = r.nextInt(knownSkills.size());
        Iterator<Skill> si = knownSkills.iterator();
        for (int j = 0; j < next; j++) {
            si.next();
        }

        return si.next();
    }
}
